var scode="";
var iTimeout=false;
var countdown = 0;
var sending = false;
$(document).ready(function(){
	var url=window.location.href;
	var index = url .lastIndexOf("\/");
	url  = url .substring(0,index + 1)+"login";
	/* 发送验证码 */
	 countdown = $.cookie('validateCodeCountdown');
    if (countdown>0) {
        sending = true;
        var obj = $("#buttn");
        obj.attr("disabled", true);
        $("#vl").text("重新发送(" + countdown + ")");
        settime();
    }

    $(window).on('beforeunload unload', function () {
        if (sending) {
            console.log(countdown);
            $.cookie('validateCodeCountdown', countdown);
        }
        if (!sending) {
            console.log(countdown);
            $.cookie('validateCodeCountdown', 0);
        }
    });
    /*点击下一步*/
	$("#next").click(function(){
		var eamil = $("#email").val();
		var reg = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if($("#username").val()==null||$("#username").val()==""){
			layer.msg('用户名不能为空', {icon: 2});
		}else if(eamil==null||eamil==""){
			layer.msg('邮箱不能为空', {icon: 2});
		}else if(reg.test(eamil)){
			$("#next").css("cursor","not-allowed");
			$("#next").attr("disabled",true);
			layer.msg("正在发送验证码至您的邮箱~", {icon: 1});
			var params = {
					'username' : $("#username").val(),
					'email' : eamil
				};
				$.ajax({
					url : "sendEmail",
					type : "POST",
					data : params,
					dataType : 'json',
					success : function(data) {
						console.log(data);
						if(data.status==200){
							$("#resertpasswd,#submit,#getcode").css("display","block");
							$("#next,#check").css("display","none");
							$("#uid").val(data.id);
							if(countdown==0){
								countdown = 120;
								sending = true;
								settime();
							}
							layer.msg(data.result, {icon: 1});
							scode=data.code;
							setTimeout(function(){
								iTimeout=true;
								scode="";
							},300000);
						}else{
							$("#next").css("cursor","");
							$("#next").attr("disabled",false);
							layer.msg(data.result, {icon: 2});
						}
						
					},
					error : function() {
							$("#next").css("cursor","");
							$("#next").attr("disabled",false);
							layer.msg('服务器发生错误,请重新操作', {icon: 5});
					}
				});
		}else{
			layer.msg('请输入正确的邮箱格式~', {icon: 2});
		}
		});
	/*点击确认*/
	$("#submit").click(function(){
		code=$("#code").val();
		username=$("#username").val();
		passwd=$("#password").val();
		cpasswd=$("#cpassword").val();
		if(code!=null&&code!=""&&username!=null&&username!=""){
			var ccode =md5(md5(username+code));
			if(iTimeout==true){
				layer.msg('验证码已过期请重新获取验证码~', {icon: 2});
				return;
			}else if(scode==""||scode==null){
				layer.msg('请获取验证码~', {icon: 2});
				return;
			}else if(ccode==scode){
				if(passwd==""||passwd==""){
					layer.msg('请输入密码~', {icon: 2});
					return;
				}else{
					if(cpasswd==""||cpasswd==""){
						layer.msg('请输入确认密码~', {icon: 2});
						return;
					}else{
						$("#submit").css("cursor","not-allowed");
						$("#submit").attr("disabled",true);
						var params = {
								'id'  :  $("#uid").val(),
								'username' : $("#username").val(),
								'password' : passwd
							};
							$.ajax({
								url : "pwdByEmail",
								type : "POST",
								data : params,
								dataType : 'json',
								success : function(data) {
									if(data.status==200){
										//$.cookie('validateCodeCountdown', 0);
										var t=3;
										setInterval(function() {
											layer.msg('修改密码成功,'+t+'秒之后跳转登陆页面~', {icon: 1});
											t--;
										}, 1000);
										setTimeout(function(){
											window.location.href=url;
										},3000);
									}else{
										$("#submit").css("cursor","not-allowed");
										$("#submit").attr("disabled",true);
										layer.msg('修改密码失败,请重试或联系管理员~', {icon: 2});
									}
									
								},
								error : function() {
										$("#submit").css("cursor","");
										$("#submit").attr("disabled",false);
										layer.msg('服务器发生错误,请重新操作', {icon: 5});
								}
							});
					}
				}
		    }else{
		    	layer.msg('验证码错误~', {icon: 5});
		    	return;
			}
		}else{
			layer.msg('请输入验证码~', {icon: 4});
			return;
			}
		});
});
/*点击发送*/
function send() {
	settime();
	$("#next").click();
}

function settime() { //发送验证码倒计时
	console.log(countdown);
    if (countdown == 0) {
    	$("#buttn,#next").attr('disabled', false);
    	$("#buttn,#next").css('cursor', "");
    	$("#vl").text("重新发送验证码");
    	$("#next").text("下一步");
        countdown = 120;
        sending = false;
        return;
    } else {
    	$("#buttn,#next").attr('disabled', true);
    	$("#buttn,#next").css('cursor', "not-allowed");
    	$("#vl").text("重新发送(" + countdown + ")");
    	$("#next").text("下一步(" + countdown + ")");
        countdown--;
    }
    setTimeout(function () {
        settime();
    }, 1000)
}